package com.yycx.module.bus.provider.handler.haiying.order;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.module.export.DictDataVo;
import com.yycx.common.base.module.export.ExportField;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.BusHyorderSendinfo;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.enums.HySaleTypeEnum;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.hy.BusHyorderSendinfoService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 海鹰订单列表
 *
 * @author zyf
 */
@Component
public class HyOrderTab2ListHandler implements PageInterceptor<HyOrder>, ExportInterceptor {


    @Autowired
    private HyOrderService orderService;

    @Autowired
    private BusDeptService deptService;

    @Autowired
    private BusCustomService customService;


    @Autowired
    private BusHyorderSendinfoService hyorderSendinfoService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery<HyOrder> cq, PageParams pageParams, EntityMap params) {
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        String deptId = params.get("deptId");
        String nickName = params.get("nickName");
        String orderNo = params.get("orderNo");
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "ho.sendDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "ho.sendDate", endDate);
        cq.eq(FlymeUtils.isNotEmpty(deptId), "ho.deptId", deptId);
        cq.like(HyOrder.class, "saleManName");
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.likeLeft("ho.orderNo", orderNo).or().likeLeft("ho.outOrderNo", orderNo));
        }
        cq.eq(true,"ho.orderType", HySaleTypeEnum.CHA.getCode());
        OpenUser openUser = OpenHelper.getUser();
        String userName = openUser.getUsername();
        cq.eq("ho.companyId", OpenHelper.getCompanyId());
        if (!userName.contains("admin") && FlymeUtils.isEmpty(nickName)) {
            cq.eq("ho.userId", openUser.getUserId());
        }
        if (FlymeUtils.isNotEmpty(nickName)) {
            cq.eq(true, "ho.nickName", nickName);
        }
        cq.eq("orderState", 2);
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrder> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            Integer sendType = entityMap.getInt("sendType");
            Date sendDate = entityMap.get("sendDate");
            String orderId = entityMap.get("orderId");
            Long deptId=entityMap.getLong("deptId");
            Long customId=entityMap.getLong("customId");
            BusDept dept=deptService.getById(deptId);
            BusCustom custom=customService.getById(customId);
            entityMap.put("deptName", dept.getDeptName());
            entityMap.put("deptType", dept.getDeptType());
            if(FlymeUtils.isNotEmpty(custom)){
                entityMap.put("customName", custom.getCustomName());
            }
            //查询退货单
            List<HyOrder> backOrderList = orderService.getBackOrder(orderId);
            String backOrderNo = "";
            if (FlymeUtils.isNotEmpty(backOrderList)) {
                backOrderNo = backOrderList.get(0).getOrderNo();
            }
            entityMap.put("backOrderNo", backOrderNo);
            //开票信息
            BusHyorderSendinfo sendInfo = hyorderSendinfoService.findByOrderId(orderId);
            if(FlymeUtils.isNotEmpty(sendInfo)){
                entityMap.put("invoiceNo", sendInfo.getInvoiceNo());
                entityMap.put("invoiceDate", DateUtils.formatDate(sendInfo.getInvoiceDate()));
                entityMap.put("fastMailName", sendInfo.getFastMailName());
                entityMap.put("fastMailNo", sendInfo.getFastMailNo());
                entityMap.put("invoiceFastMailName", sendInfo.getInvoiceFastMailName());
                entityMap.put("invoiceFastMailNo", sendInfo.getInvoiceFastMailNo());
            }
            if (sendType.equals(3)) {
                Integer reputation = entityMap.getInt("reputation");
                if (FlymeUtils.isNotEmpty(sendDate) && reputation < 1000 && reputation > 0) {
                    Date reputationDate = DateUtils.addDays(sendDate, reputation);
                    entityMap.put("reputationDate", DateUtils.formatDate(reputationDate, "yyyy-MM-dd"));
                    Integer dayNum = DateUtils.daysBetween(DateUtils.getNowDate(), reputationDate);
                    entityMap.put("dayNum", dayNum);
                } else {
                    entityMap.put("reputationDate", "");
                    entityMap.put("dayNum", -1);
                }
            } else {
                entityMap.put("reputationDate", "-");
                entityMap.put("dayNum", -1);
            }
        }
        return result;
    }
    @Override
    public void initExcelExportEntity(ExportField exportField, List list) {
        Object key = exportField.getKey();
        if (key.equals("invoiceType")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("专票");
            dictDataVo.setValue(2);
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("普票");
            dictDataVo2.setValue(1);
            dictDataVos.add(dictDataVo);
            dictDataVos.add(dictDataVo2);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }
        if (key.equals("deptType")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("招商");
            dictDataVo.setValue("ZS");
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("自营");
            dictDataVo2.setValue("ZY");
            dictDataVos.add(dictDataVo);
            dictDataVos.add(dictDataVo2);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }
        if (key.equals("sendType")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("现款");
            dictDataVo.setValue(1);
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("预收款");
            dictDataVo2.setValue(2);
            dictDataVos.add(dictDataVo);


            DictDataVo dictDataVo3 = new DictDataVo();
            dictDataVo3.setLabel("资保金");
            dictDataVo3.setValue(3);
            dictDataVos.add(dictDataVo3);

            DictDataVo dictDataVo4 = new DictDataVo();
            dictDataVo3.setLabel("全额资信");
            dictDataVo3.setValue(4);
            dictDataVos.add(dictDataVo4);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }

        exportField.setWidth(20);

    }

}
